<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This file documents the use of the GNU compilers.

Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Temporaries (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="Temporaries (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="Temporaries (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Indices.html" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="C_002b_002b-Misunderstandings.html" rel="up" title="C++ Misunderstandings">
<link href="Copy-Assignment.html" rel="next" title="Copy Assignment">
<link href="Name-lookup.html" rel="prev" title="Name lookup">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
kbd.key {font-style: normal}
span:hover a.copiable-link {visibility: visible}
-->
</style>


</head>

<body lang="en_US">
<div class="subsection-level-extent" id="Temporaries">
<div class="nav-panel">
<p>
Next: <a href="Copy-Assignment.html" accesskey="n" rel="next">Implicit Copy-Assignment for Virtual Bases</a>, Previous: <a href="Name-lookup.html" accesskey="p" rel="prev">Name Lookup, Templates, and Accessing Members of Base Classes</a>, Up: <a href="C_002b_002b-Misunderstandings.html" accesskey="u" rel="up">Common Misunderstandings with GNU C++</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Temporaries-May-Vanish-Before-You-Expect"><span>14.7.3 Temporaries May Vanish Before You Expect<a class="copiable-link" href="#Temporaries-May-Vanish-Before-You-Expect"> &para;</a></span></h4>

<a class="index-entry-id" id="index-temporaries_002c-lifetime-of"></a>
<a class="index-entry-id" id="index-portions-of-temporary-objects_002c-pointers-to"></a>
<p>It is dangerous to use pointers or references to <em class="emph">portions</em> of a
temporary object.  The compiler may very well delete the object before
you expect it to, leaving a pointer to garbage.  The most common place
where this problem crops up is in classes like string classes,
especially ones that define a conversion function to type <code class="code">char *</code>
or <code class="code">const char *</code>&mdash;which is one reason why the standard
<code class="code">string</code> class requires you to call the <code class="code">c_str</code> member
function.  However, any class that returns a pointer to some internal
structure is potentially subject to this problem.
</p>
<p>For example, a program may use a function <code class="code">strfunc</code> that returns
<code class="code">string</code> objects, and another function <code class="code">charfunc</code> that
operates on pointers to <code class="code">char</code>:
</p>
<div class="example smallexample">
<pre class="example-preformatted">string strfunc ();
void charfunc (const char *);

void
f ()
{
  const char *p = strfunc().c_str();
  ...
  charfunc (p);
  ...
  charfunc (p);
}
</pre></div>

<p>In this situation, it may seem reasonable to save a pointer to the C
string returned by the <code class="code">c_str</code> member function and use that rather
than call <code class="code">c_str</code> repeatedly.  However, the temporary string
created by the call to <code class="code">strfunc</code> is destroyed after <code class="code">p</code> is
initialized, at which point <code class="code">p</code> is left pointing to freed memory.
</p>
<p>Code like this may run successfully under some other compilers,
particularly obsolete cfront-based compilers that delete temporaries
along with normal local variables.  However, the GNU C++ behavior is
standard-conforming, so if your program depends on late destruction of
temporaries it is not portable.
</p>
<p>The safe way to write such code is to give the temporary a name, which
forces it to remain until the end of the scope of the name.  For
example:
</p>
<div class="example smallexample">
<pre class="example-preformatted">const string&amp; tmp = strfunc ();
charfunc (tmp.c_str ());
</pre></div>

</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Copy-Assignment.html">Implicit Copy-Assignment for Virtual Bases</a>, Previous: <a href="Name-lookup.html">Name Lookup, Templates, and Accessing Members of Base Classes</a>, Up: <a href="C_002b_002b-Misunderstandings.html">Common Misunderstandings with GNU C++</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
